package com.fjnu.config;

import com.fjnu.annotation.NPECatch;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @desc: 异常捕获切面
 */
@Aspect
@Component
public class ExceptionAspect {

    /**
     * 切入点：带有@NPECatch注解的方法
     */
    @Pointcut("@within(com.fjnu.annotation.NPECatch) || @annotation(com.fjnu.annotation.NPECatch)")
    public void npeCatchMethods() {}

    /**
     * 环绕通知：捕获异常并输出信息
     */
    @Around("npeCatchMethods()")
    public Object handleException(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().getName();

        try {
            return joinPoint.proceed();
        } catch (Exception e) {
            // 输出方法名和异常信息到控制台
            System.out.printf("[NPECatch] Method '%s' threw exception: %s%n", methodName, e.getMessage());
            throw e; // 重新抛出异常
        }
    }
}